package name.jiaosha.air.system.controller;

import java.util.ArrayList;
import java.util.List;

import name.jiaosha.air.AppConstants;
import name.jiaosha.air.system.domain.Category;
import name.jiaosha.air.system.service.ISystemService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ali8ba.common.PageInfo;
import com.ali8ba.common.PinyinToolkit;

/**
 * 类别控制器
 * 
 * @author xu
 * 
 */
@Controller
@RequestMapping("/admin/sys")
public class CategoryController {

	@Autowired
	private ISystemService systemService;

	@RequestMapping("/categoryList")
	public ModelAndView getMyBugList() {

		PageInfo<Category> pageInfo = null;

		if (!AppConstants.DEBUG) {
			List<Category> list = new ArrayList<Category>();
			for (int i = 1; i <= 20; i++) {
				Category category = new Category();
				category.setId(i);
				category.setName("柜机" + i);
				category.setPinyin(PinyinToolkit.cn2FirstSpell(category
						.getName()));
				list.add(category);
			}

		} else {

			pageInfo = systemService.getCategoryList(1,
					AppConstants.DEFAULT_SIZE);
		}

		ModelMap model = new ModelMap();
		model.addAttribute("root", pageInfo.getItemList());
		model.addAttribute("totalProperty", pageInfo.getTotalProperty());
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/createCategory")
	public ModelAndView createCategory(@RequestParam String name) {

		if (!AppConstants.DEBUG) {

		} else {

			Category category = new Category();
			category.setName(name.trim());
			systemService.createCategory(category);

		}

		ModelMap model = new ModelMap();
		model.addAttribute("success", "true");
		return new ModelAndView("jsonView", model);
	}

	@RequestMapping("/updateCategory")
	public ModelAndView updateCategory(@RequestParam int id,
			@RequestParam String name) {

		if (!AppConstants.DEBUG) {

		} else {

			Category category = systemService.getCategoryById(id);

			if (category != null) {

				category.setName(name.trim());
				systemService.updateCategory(category);
			}

		}

		ModelMap model = new ModelMap();
		model.addAttribute("success", "true");
		return new ModelAndView("jsonView", model);
	}

}
